/**
 * 公共的layer弹出框
 */
layui.define(['jquery','layer','form','view','table','treeGrid'], function(exports){
    var $ = layui.$
        ,layer = layui.layer
        ,view = layui.view
        ,table = layui.table
        ,treeGrid = layui.treeGrid
        ,form = layui.form;
    var layers = {
        //普通表格默认属性
        add:function (params) {
            var defaultOpt = {
                type: 2,
                treeTable:null,
                title: '新增',
                content: '/console/'+($("#modleName").val() || '') + "/preAdd",
                area:['500px', '300px'],
                btn:['提交', '关闭'],
                success:function(){
                    window._data = params.data;
                },
                yes: function(index, layero){
                    var body = layer.getChildFrame('body', index);
                    var $form = body.find('.layui-form.layui-form-pane');
                    //扩展需要在表单提交之前的方法
                    if($form[0].onsubmit){
                        $form[0].onsubmit();
                    }
                    //添加监听submit事件
                    var iframeWin = window['layui-layer-iframe'+index];
                    iframeWin.layui.form.on('submit(*)', function(data){
                        view.req({
                            url: data.form.action
                            ,data: data.field
                            ,type: 'post'
                            ,index: index
                            ,done: function(res){
                                layer.msg(res.msg);
                                if(defaultOpt.treeTable == null){
                                    table.reload("lay-table");
                                }else{
                                    treeGrid.query("lay-table",{
                                        where: defaultOpt.treeTable.where
                                    });
                                }
                                p.data = {};
                            }
                        });
                        return false;
                    });

                    // 找到隐藏的提交按钮模拟点击提交
                    body.find('#permissionSubmit').click();
                },
                btn2: function(index, layero){//关闭
                    layer.close(index);
                }
            };
            defaultOpt=$.extend({},defaultOpt,params);
           return layer.open(defaultOpt);
        },
        edit:function (params) {
            if($.isEmptyObject(params.data)){
                layer.msg("请先选择一条数据");
                return;
            }
            var defaultOpt = {
                type: 2,
                treeTable:null,
                title: '编辑',
                content: '/console/'+($("#modleName").val() || '') + "/preModify",
                area:['500px', '300px'],
                btn:['提交', '关闭'],
                success:function(){
                    window._data = params.data;
                },
                yes: function(index, layero){
                    var body = layer.getChildFrame('body', index);
                    var $form = body.find('.layui-form.layui-form-pane');
                    //扩展需要在表单提交之前的方法
                    if($form[0].onsubmit){
                        $form[0].onsubmit();
                    }
                    //添加监听submit事件
                    var iframeWin = window['layui-layer-iframe'+index];

                    iframeWin.layui.form.on('submit(*)', function(data){
                        view.req({
                            url: data.form.action
                            ,data: data.field
                            ,type: 'post'
                            ,index: index
                            ,done: function(res){
                                layer.msg(res.msg);
                                if(defaultOpt.treeTable == null){
                                    table.reload("lay-table");
                                }else{
                                    treeGrid.query("lay-table",{
                                        where: defaultOpt.treeTable.where
                                    });
                                }
                                p.data = {};
                            }
                        });
                        return false;
                    });
                    // 找到隐藏的提交按钮模拟点击提交
                    body.find('#permissionSubmit').click();
                },
                btn2: function(index){//关闭
                    layer.close(index);
                }
            };
            defaultOpt=$.extend({},defaultOpt,params);
            return layer.open(defaultOpt);
        },
        delete:function(params){
            if($.isEmptyObject(params.data)){
                layer.msg("请先选择一条数据");
                return;
            }
            var defaultOpt = {
                url: '/console/'+($("#className").val() || '') + '/delete' //实际使用请改成服务端真实接口
                ,data: {id:params.data.id}
                ,treeTable:null
                ,type: 'post'
                ,done: function(res){
                    layer.msg(res.msg);
                    if(defaultOpt.treeTable == null){
                        table.reload("lay-table");
                    }else{
                        treeGrid.query("lay-table",{
                            where: defaultOpt.treeTable.where
                        });
                    }
                    p.data = {};
                }
            };
            defaultOpt=$.extend({},defaultOpt,params);
            layer.confirm(params.msg? params.msg : '确定删除吗？', function(index){
                view.req(defaultOpt);
                layer.close(index);
            });

        },
        view:function (params) {
            if($.isEmptyObject(params.data)){
                layer.msg("请先选择一条数据");
                return;
            }
            var defaultOpt = {
                type: 2,
                title: '查看',
                content: '/console/'+($('#modleName').val() || '') + '/view',
                area:['500px', '300px'],
                btn:['关闭'],
                success:function(){
                    window._data = params.data;
                },
                yes: function(index){
                    layer.close(index);
                }
            };
            defaultOpt=$.extend({},defaultOpt,params);
            return layer.open(defaultOpt);
        },
        openWindowInHidden: function (opts) {
            opts = $.extend({
                url: '',
                method: 'POST',
                target: '',
                queryParams: {}
            }, opts);
            if (!opts.url) return;
            if (!opts.target && 0 >= $('#hideFrameOfOpenWindow').length) {
                $('<iframe id="hideFrameOfOpenWindow" name="hideFrameOfOpenWindow"></iframe>').hide().appendTo(document.body);
            }
            if ((opts.method || '').toUpperCase() === 'GET') {
                var url = opts.url || '', defaultOpt = $.isPlainObject(opts.queryParams) ? $.params(opts.queryParams, true) : opts.queryParams;
                if (!!defaultOpt) url += (0 > url.indexOf('?') ? '?' : '&') + defaultOpt;
                window.open(url, !!opts.target && opts.target || 'hideFrameOfOpenWindow');
            } else {
                var $form = $('#hideFormOfOpenWindow'), val;
                if (0 >= $form.length) {
                    $form = $('<form id="hideFormOfOpenWindow" method="post" target="hideFrameOfOpenWindow"></form>');
                    $form.hide().appendTo(document.body);
                }
                !!opts.target && $form.attr('target', opts.target);
                $form.empty();
                createHidden('', opts.queryParams);
                $form.attr('action', opts.url);
                $form[0].submit();
                function createHidden(key, val) {
                    if (!!key && (val === undefined || val === null)) {
                        $('<input type="hidden"/>').attr('name', key).appendTo($form);
                    } else if ($.isArray(val)) {
                        $.each(val, function () {
                            createHidden(key, this);
                        });
                    } else if ($.isPlainObject(val)) {
                        for (var name in val) {
                            createHidden((!!key && (key + '.') || '') + name, val[name]);
                        }
                    } else if (!!key && !$.isFunction(val)) {
                        $('<input type="hidden"/>').attr('name', key).val(val).appendTo($form);
                    }
                }
            }
        }
    };

    exports('layers', layers)
});